package com.zhongfl.crm.api.view;

import com.github.pagehelper.PageInfo;
import com.zhongfl.crm.model.LxCustomerOrder;
import com.zhongfl.crm.model.LxDepartment;
import com.zhongfl.crm.model.OrderType;
import com.zhongfl.crm.request.LxCustomerOrderRequest;
import com.zhongfl.crm.service.LxCustomerOrderService;
import com.zhongfl.crm.service.LxDepartmentService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.heptagram.core.common.rest.ResponseResult;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 励销客户订单api
 *
 * @author 奈良鹿丸
 * @date 2018/9/20
 */

@RestController
@RequestMapping("/lxCustomerOrder")
public class LxCustomerOrderController {

    @Resource
    private LxCustomerOrderService lxCustomerOrderService;

    @Resource
    private LxDepartmentService lxDepartmentService;

    @RequestMapping("/findLxCustomerOrderByPage")
    public ResponseResult<PageInfo<LxCustomerOrder>> findLxCustomerOrderByPage(@RequestBody LxCustomerOrderRequest lxCustomerOrderRequest) {
        //获取当前巴及其子巴id集合,并放入lxCustomerOrderRequest对象中
        List list = new ArrayList();
        List<LxDepartment> lxDepartments = new ArrayList<>();
        if(lxCustomerOrderRequest.getDepartmentId() != null && lxDepartmentService.findByLxId(lxCustomerOrderRequest.getDepartmentId()).getLevel() != 0){
             lxDepartments = lxDepartmentService.findByParentId(lxCustomerOrderRequest.getDepartmentId());
        }else if(lxCustomerOrderRequest.getDepartmentId() != null){
            lxDepartments = lxDepartmentService.findAllDepartment();
        }
        list.add(lxCustomerOrderRequest.getDepartmentId());
        for (LxDepartment lxDepartment : lxDepartments) {
            list.add(lxDepartment.getLxId());
        }
        lxCustomerOrderRequest.setDepartmentIds(list);

        List<LxCustomerOrder> lxCustomerOrders = lxCustomerOrderService.findLxCustomerOrderByPage(lxCustomerOrderRequest);
        PageInfo<LxCustomerOrder> lxCustomerOrderPageInfo = new PageInfo<>(lxCustomerOrders);
        return new ResponseResult<>(lxCustomerOrderPageInfo);
    }

    @RequestMapping("/findAllStage")
    public ResponseResult<List<OrderType>> findAllStage() {

        List<OrderType> stages = lxCustomerOrderService.findAllStage();
        return new ResponseResult<>(stages);
    }

    @RequestMapping("/findLxCustomerOrderById")
    public ResponseResult<LxCustomerOrder> findLxCustomerOrderById(Long id) {
        LxCustomerOrder lxCustomerOrder = lxCustomerOrderService.findById(id);
        return new ResponseResult<>(lxCustomerOrder);
    }

    /**
     * 按励销渠道id查询客户订单
     */
    @PostMapping("/findLxCustomerOrderByCustomerId")
    public ResponseResult<PageInfo<LxCustomerOrder>> findLxCustomerOrderByCustomerId(@RequestBody LxCustomerOrderRequest lxCustomerOrderRequest) {
        return new ResponseResult<>(new PageInfo<>(lxCustomerOrderService.findLxCustomerOrderByCustomerId(lxCustomerOrderRequest)));
    }
}
